package com.dingwei.superiordiary.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.RegexUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/1.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class ScanActivity extends BaseActivity {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.fl_my_container)
    FrameLayout flMyContainer;
    @Bind(R.id.tv_light)
    TextView tvLight;

    private boolean isLight=false;

    public static  int REQUEST_IMAGE =1002;
    @Override
    protected int getContentViewId() {
        return R.layout.activty_scan;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        tvTitle.setText("二维码");
        tvRight.setText("相册");

        /**
         * 执行扫面Fragment的初始化操作
         */
        CaptureFragment captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);

        captureFragment.setAnalyzeCallback(analyzeCallback);
        /**
         * 替换我们的扫描控件
         */
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();
    }
    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            if (RegexUtils.isURL(result)){
                intent = new Intent(BaseActivity.context, WebViewActivity.class);
                intent.putExtra("url", result);
                intent.putExtra("title","详情");
                startActivity(intent);
            }else {
                showToast(result);
            }
        }

        @Override
        public void onAnalyzeFailed() {
            showToast("扫描失败");
        }
    };
    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.tv_right, R.id.tv_light})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                PictureSelector.create(ScanActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .theme(R.style.picture_white_style)
                        .imageSpanCount(3)
                        .selectionMode(PictureConfig.SINGLE)
                        .previewImage(true)
                        .isCamera(true)
                        .isZoomAnim(true)
                        .enableCrop(false)// 是否裁剪 true or false
                        .compress(false)// 是否压缩 true or false
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.tv_light:
                if (!isLight){
                    CodeUtils.isLightEnable(true);
                    isLight = true;
                }else {
                    CodeUtils.isLightEnable(false);
                    isLight = false;
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == PictureConfig.CHOOSE_REQUEST){
            // 图片选择结果回调
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            // 例如 LocalMedia 里面返回三种path
            // 1.media.getPath(); 为原图path
            // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
            // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
            // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
            String lastPath = selectList.get(0).getPath();
            try {

                CodeUtils.analyzeBitmap(lastPath, new CodeUtils.AnalyzeCallback() {
                    @Override
                    public void onAnalyzeSuccess(Bitmap mBitmap, String result) {

                        showToast(result);
                    }

                    @Override
                    public void onAnalyzeFailed() {
                        showToast("解析二维码失败");
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
